/**
 * uni-app 通用API封装
 */

// 显示消息提示框
export const showToast = (title, icon = 'none', duration = 2000) => {
  return uni.showToast({
    title,
    icon,
    duration
  })
}

// 显示 loading 提示框
export const showLoading = (title = '加载中...', mask = true) => {
  return uni.showLoading({
    title,
    mask
  })
}

// 隐藏 loading 提示框
export const hideLoading = () => {
  return uni.hideLoading()
}

// 显示模态弹窗
export const showModal = (title, content, showCancel = true) => {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title,
      content,
      showCancel,
      success: (res) => {
        resolve(res)
      },
      fail: reject
    })
  })
}

// 页面跳转
export const navigateTo = (url) => {
  return uni.navigateTo({ url })
}

// 重定向到页面
export const redirectTo = (url) => {
  return uni.redirectTo({ url })
}

// 返回上一页
export const navigateBack = (delta = 1) => {
  return uni.navigateBack({ delta })
}

// 显示操作菜单
export const showActionSheet = (itemList) => {
  return new Promise((resolve, reject) => {
    uni.showActionSheet({
      itemList,
      success: (res) => {
        resolve(res.tapIndex)
      },
      fail: reject
    })
  })
}

// 扫码
export const scanCode = (onlyFromCamera = true, scanType = ['qrCode', 'barCode']) => {
  return new Promise((resolve, reject) => {
    uni.scanCode({
      onlyFromCamera,
      scanType,
      success: (res) => {
        resolve(res)
      },
      fail: reject
    })
  })
}

// 设置存储
export const setStorage = (key, data) => {
  return new Promise((resolve, reject) => {
    uni.setStorage({
      key,
      data,
      success: resolve,
      fail: reject
    })
  })
}

// 获取存储
export const getStorage = (key) => {
  return new Promise((resolve, reject) => {
    uni.getStorage({
      key,
      success: (res) => {
        resolve(res.data)
      },
      fail: reject
    })
  })
}

// 删除存储
export const removeStorage = (key) => {
  return new Promise((resolve, reject) => {
    uni.removeStorage({
      key,
      success: resolve,
      fail: reject
    })
  })
}